<?php
/**
 * OA_Loader
 * override CI_Loader, use smarty instead of php template
 * 
 * $Id: OA_Loader.php 159 2011-09-27 16:19:32Z alpha1130@gmail.com $
 */

class OA_Loader extends CI_Loader
{
	/**
	 * Smarty
	 * 
	 * @var Smarty
	 */
	private $smarty;
	
	/**
	 * load smarty
	 * default load smarty path application/libraries/smarty
	 *
	 * @return Smarty
	 */
	private function &_load_smarty()
	{
		if($this->smarty == NULL)
		{
			$ci =& get_instance();
			$config = $ci->config->item('smarty');
			if($config)
			{
				class_exists('Smarty') || require $config['class_file'];
				$this->smarty = new Smarty();
				
				foreach($config as $k => $v)
				{
					switch($k)
					{
						case 'template_dir':
							$this->smarty->setTemplateDir($v);
							break;
						case 'plugins_dir':
							$this->smarty->addPluginsDir($v);
							break;
						case 'class_file': // skip custom smarty class define
							break;
						default:
							$this->smarty->$k = $v;
					}
				}
			}
		}
		
		return $this->smarty;
	}
	
	function view($view, $vars = array(), $return = FALSE)
	{
		// load smarty
		$smarty =& $this->_load_smarty();
		
		// prevent reload vars
		$smarty->clearAllAssign();
		$smarty->assign($vars + $this->_ci_cached_vars);
		
		// auto append '.tpl' suffix
		if(substr($view, -4) !== '.tpl')
		{
			$view .= '.tpl';
		}
		
		if($return)
		{
			return $smarty->fetch($view);
		}
		
		$ci =& get_instance();
		$ci->output->append_output($smarty->fetch($view));
	}
	
}

/* End of file: OA_Loader.php */
/* Location: ./application/core/OA_Loader.php */
